/**
 * Created by Maxwell on 2016/9/27.
 */
"use strict";
let router = require('koa-router')();
let config = require("../../config");
let homeService = require('../../service/home/service');

router.get('/', function *() {
    this.redirect('index');
});

/**
 * 首页
 */
router.get('/index', function *() {
    var clientToken = this.session.clientToken;
    var item1 = yield homeService.getItemPages({
        ShowType: 1,
        Page: 1,
        PageSize: 10,
        token: clientToken['access_token']
    });
    var item2 = yield homeService.getItemPages({
        ShowType: 2,
        Page: 1,
        PageSize: 10,
        token: clientToken['access_token']
    });
    var item3 = yield homeService.getItemPages({
        ShowType: 3,
        Page: 1,
        PageSize: 10,
        token: clientToken['access_token']
    });
    var item4 = yield homeService.getItemPages({
        ShowType: 4,
        Page: 1,
        PageSize: 10,
        token: clientToken['access_token']
    });
    
    function genProcess(item) {
        for(var i = 0;i<item.length;i++){
            item[i]['process'] = ((parseInt(item[i]['JoinPersonTimes'])/parseInt(item[i]['PersonTimes']))* 100).toFixed(0)
        }
        return item;
    }

    try {
        yield this.render('index', {item1: genProcess(item1), item2: genProcess(item2), item3: genProcess(item3), item4: genProcess(item4), uri: config.apiHost.uri});
    } catch (e) {
        this.redirect('error/sysError');
        throw new Error(e);
    }
});


/**
 * 加载下一页
 */
router.post('/getItemPagesIF' ,function *(){
    var params = this.request.body;
    var clientToken = this.session.clientToken;
    var item = yield homeService.getItemPages({
        ShowType: params['ShowType'],
        Page: params['Page'],
        PageSize: 10,
        token: clientToken['access_token']
    });
    this.body = item;
});
module.exports = router;